define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'app/intelattachment/index' + location.search,
                    add_url: 'app/intelattachment/add',
                    edit_url: 'app/intelattachment/edit',
                    del_url: 'app/intelattachment/del',
                    delall_url: 'app/intelattachment/delall',
                    multi_url: 'app/intelattachment/multi',
                    table: 'app_intel_attachment',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'url', title: __('Url'), formatter: Table.api.formatter.url},
                        {field: 'imagetype', title: __('Imagetype')},
                        {field: 'filesize', title: __('Filesize')},
                        {field: 'uploadtime', title: __('Uploadtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'storage', title: __('Storage')},
                        {field: 'user.nickname', title: __('User.nickname')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 批量删除按钮事件
            $('.toolbar').on('click', '.btn-delall', function () {
                var that = this;
                Layer.confirm('是否一键清理无效图片？（1天前且无绑定情报的图片）',
                    {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                    function (index) {
                        Fast.api.ajax($.fn.bootstrapTable.defaults.extend.delall_url, function () {
                            Layer.close(index);
                            table.bootstrapTable('refresh');
                        });
                    }
                );
            });
        }
    };
    return Controller;
});
